package com.yyks.es.dubbo.service.hotel;

import cn.hutool.json.JSONUtil;
import com.yeyks.common.utils.SystemStrackUtil;
import com.yyks.es.dubbo.constants.EsIndexConstant;
import com.yyks.es.dubbo.pojo.model.EsHotelInfo;
import com.yyks.es.dubbo.pojo.model.EsRoomCalendarInfo;
import com.yyks.es.dubbo.pojo.model.EsRoomInfo;
import org.apache.dubbo.config.annotation.Service;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.join.query.JoinQueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EsSearchBaseServiceImpl implements EsSearchBaseService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    //v3.6 搜索酒店
    public EsHotelInfo selectHotel(String sourceHotelId, String sourceType) {
        try {
            SearchResponse searchResponse = restHighLevelClient.search(
                    new SearchRequest(EsIndexConstant.INDEX_HOTEL)
                            .types(EsIndexConstant.NORMAL_TYPE)
                            .source(
                                    new SearchSourceBuilder()
                                            .size(1)
                                            .query(
                                                    QueryBuilders.boolQuery()
                                                            .must(QueryBuilders.termQuery("sourceId", sourceHotelId))
                                                            .must(QueryBuilders.termQuery("sourceType", sourceType))
                                                            .must(JoinQueryBuilders.hasChildQuery(EsIndexConstant.JOIN_TYPE_ROOM, QueryBuilders.matchAllQuery(), ScoreMode.None))
                                            )
                            )
            );
            return JSONUtil.toBean(searchResponse.getHits().getHits()[0].getSourceAsString(), EsHotelInfo.class);
        } catch (Exception e) {
            SystemStrackUtil.logYyksExceptionStack(e);
        }
        return null;
    }

    //v3.6 搜索房间
    public EsRoomInfo selectRoom(String sourceRoomlId, String sourceType) {
        try {
            SearchResponse searchResponse = restHighLevelClient.search(
                    new SearchRequest(EsIndexConstant.INDEX_HOTEL)
                            .types(EsIndexConstant.NORMAL_TYPE)
                            .source(
                                    new SearchSourceBuilder()
                                            .size(1)
                                            .query(
                                                    QueryBuilders.boolQuery()
                                                            .must(QueryBuilders.termQuery("sourceId", sourceRoomlId))
                                                            .must(QueryBuilders.termQuery("sourceType", sourceType))
                                                            .must(JoinQueryBuilders.hasParentQuery(EsIndexConstant.JOIN_TYPE_HOTEL, QueryBuilders.matchAllQuery(), false))
                                            )
                            )
            );
            return JSONUtil.toBean(searchResponse.getHits().getHits()[0].getSourceAsString(), EsRoomInfo.class);
        } catch (Exception e) {
            SystemStrackUtil.logYyksExceptionStack(e);
        }
        return null;
    }

    //v3.6 搜索单个房态
    public EsRoomCalendarInfo selectCalendar(String sourceCalendarlId, String sourceType, Date date) {
        try {
            SearchResponse searchResponse = restHighLevelClient.search(
                    new SearchRequest(EsIndexConstant.INDEX_HOTEL)
                            .types(EsIndexConstant.NORMAL_TYPE)
                            .source(
                                    new SearchSourceBuilder()
                                            .size(1)
                                            .query(
                                                    QueryBuilders.boolQuery()
                                                            .must(QueryBuilders.termQuery("sourceId", sourceCalendarlId))
                                                            .must(QueryBuilders.termQuery("sourceType", sourceType))
                                                            .must(QueryBuilders.termQuery("date", date))
                                                            .must(JoinQueryBuilders.hasParentQuery(EsIndexConstant.JOIN_TYPE_ROOM, QueryBuilders.matchAllQuery(), false))
                                            )
                            )
            );
            return JSONUtil.toBean(searchResponse.getHits().getHits()[0].getSourceAsString(), EsRoomCalendarInfo.class);
        } catch (Exception e) {
            SystemStrackUtil.logYyksExceptionStack(e);
        }
        return null;
    }

    //v3.6 搜索多个个房态,多个日历
    public List<EsRoomCalendarInfo> selectCalendar(String sourceCalendarlId, String sourceType, Date[] date) {
        try {
            SearchResponse searchResponse = restHighLevelClient.search(
                    new SearchRequest(EsIndexConstant.INDEX_HOTEL)
                            .types(EsIndexConstant.NORMAL_TYPE)
                            .source(
                                    new SearchSourceBuilder()
                                            .size(1)
                                            .query(
                                                    QueryBuilders.boolQuery()
                                                            .must(QueryBuilders.termQuery("sourceId", sourceCalendarlId))
                                                            .must(QueryBuilders.termQuery("sourceType", sourceType))
                                                            .must(QueryBuilders.termsQuery("date", date))
                                                            .must(JoinQueryBuilders.hasParentQuery(EsIndexConstant.JOIN_TYPE_ROOM, QueryBuilders.matchAllQuery(), false))
                                            )
                            )
            );
            List<EsRoomCalendarInfo> esRoomCalendarInfos = new ArrayList<>();
            for (SearchHit searchHit : searchResponse.getHits().getHits()) {
                esRoomCalendarInfos.add(JSONUtil.toBean(searchHit.getSourceAsString(), EsRoomCalendarInfo.class));
            }
            return esRoomCalendarInfos;
        } catch (Exception e) {
            SystemStrackUtil.logYyksExceptionStack(e);
        }
        return null;
    }

}
